package com.dating.base.service;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.dating.base.bean.Permission;
import com.dating.base.bean.User;
import com.dating.base.bean.User.Type;
import com.dating.base.bean.UserRole;
import com.dating.base.dao.UserDao;
import com.dating.base.dao.UserRoleDao;
import com.dating.base.dto.Menu;
import com.dating.base.dto.Pager;
import com.dating.utils.StrKit;

@IocBean
public class UserService{

	@Inject
	private UserDao userDao;
	
	@Inject
	private UserRoleDao userRoleDao;
	
	@Inject
	private ShiroUserService shiroUserService;
	
	@Inject
	private Dao  dao ;

	
	
	public User addUser(User user) {
		return userDao.save(user);
	}
	
	public boolean updateUser(User user) {
		return userDao.updateIgnoreNull(user);
	}
	
	
	public User getById(Integer id){
		return userDao.findById(id);
	}
	
	public User getUser(String uname) {
		return userDao.findByCondition(Cnd.where("name", "=", uname));
	}
	
	public boolean delUser(Integer id){
		return userDao.delById(id);
	}
	
	public boolean setUserRole(UserRole userRole, boolean setAble){
		Cnd where = Cnd.where("userId", "=" , userRole.getUserId())
				.and("roleId", "=", userRole.getRoleId())
				.and("type", "=",  userRole.getType().name());
		UserRole model = userRoleDao.findByCondition(where);
		if(setAble && model == null){
			return userRoleDao.save(userRole) != null;
		}else if(!setAble && model != null){
			return userRoleDao.delById(model.getId());
		}
		return true;
	}
	
	/**
	 * 查询用户列表
	 */
	public void getUsersByPage(Pager pager) {
		String sql = " select a.* from t_user a where 1=1 ";
		String keyword = (String)pager.getParams().get("key");
		if(StrKit.notBlank(keyword)){
			sql += " and (a.u_name like '%"+ keyword +"%' " ;
			sql += " or a.u_real_name like '%"+ keyword +"%' " ;
			sql += " or a.u_phone like '%"+ keyword +"%' " ;
			sql += " or a.u_email like '%"+ keyword +"%') " ;
		}
		if(StrKit.notBlank(pager.getOrderBy())){
			sql += " order by "+ pager.getOrderBy() +" " + pager.getSortDesc() ;
		}
		sql += (" limit " + ((pager.getPageNumber() - 1) * pager.getPageSize()) + "," + pager.getPageSize());
		Sql sqls = Sqls.queryEntity(sql);
		sqls.setEntity(userDao.getDao().getEntity(User.class));
		userDao.getDao().execute(sqls);
		List<User> users = sqls.getList(User.class);
		pager.setPageRows(users);
		pager.setRecordCount(userDao.countAll());
	}
	
	/**
	 * 查询用户所拥有的菜单
	 */
	public List<Menu> getMenus(int uid) {
		
		List<Menu> menus = new ArrayList<Menu>();
		List<Menu> topMenus = new ArrayList<Menu>();
		List<Permission> ps = shiroUserService.findAllPermission(uid, Type.PLATFORM);
		for (Permission p : ps) {
			if(p.isMenu()){
				Menu menu = new Menu(p);
				
				if(menu.getLevel() == 0){
					topMenus.add(menu);
				}
				menus.add(menu);
			}
		}
		for (Menu menu : topMenus) {
			menu.fullSubMenu(menus);
		}
		
		return topMenus;
	}
	
}
